package com.yzt.message.custom;

import android.Manifest;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.permission.PermissionsUtils;
import com.yzt.zhuju.utils.ActivityManagerUtils;
import com.yzt.zhuju.widget.GenderAgeView;
import com.yzt.p_base.utils.DisplayUtils;
import com.yzt.message.R;

import java.util.List;

import io.rong.imkit.conversation.messgelist.provider.BaseMessageItemProvider;
import io.rong.imkit.conversation.messgelist.provider.GIFMessageItemProvider;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.utils.PermissionCheckUtil;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imkit.widget.adapter.ViewHolder;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.message.GIFMessage;

/**
 * gIF 消息
 */
public class CustomGIFMessageItemProvider extends GIFMessageItemProvider {

    public CustomGIFMessageItemProvider() {
        this.mConfig.showProgress = false;
        this.mConfig.showContentBubble = false;
    }

    //如果不继承CustomBaseMessageItemProvider , 就重写这个替换布局
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.nomi_rc_message_item, parent, false);
        FrameLayout contentView = rootView.findViewById(R.id.rc_content);
        ViewHolder contentViewHolder = this.onCreateMessageContentViewHolder(contentView, viewType);
        if (contentViewHolder != null && contentView.getChildCount() == 0) {
            contentView.addView(contentViewHolder.itemView);
        }

        return new BaseMessageItemProvider.MessageViewHolder(rootView.getContext(), rootView, contentViewHolder);
    }

    @Override
    protected ViewHolder onCreateMessageContentViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nomi_rc_item_gif_message, parent, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(ViewHolder holder, ViewHolder parentHolder, GIFMessage gifMessage, UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
        super.bindMessageContentViewHolder(holder, parentHolder, gifMessage, uiMessage, position, list, listener);
        //隐藏进度view
        holder.setVisible(R.id.rc_pre_progress, false);
        holder.setVisible(R.id.rc_gif_progress, false);
        LinearLayout layoutItem = holder.getView(R.id.layout_item);
        ImageView imageView = holder.getView(R.id.rc_img);

        if (uiMessage.getConversationType() == Conversation.ConversationType.GROUP) {
            imageView.post(new Runnable() {
                @Override
                public void run() {
                    ViewGroup.LayoutParams itemLayoutParams = layoutItem.getLayoutParams();
                    itemLayoutParams.width = Math.max(imageView.getWidth(), DisplayUtils.dp2px(105));
                    layoutItem.setLayoutParams(itemLayoutParams);
                }
            });
        }

        View layoutGroup = holder.getView(R.id.layout_group);
        GenderAgeView viewGender = holder.getView(R.id.view_gender);
        ImageView ivPosition = holder.getView(R.id.iv_position);

        layoutGroup.setVisibility(View.GONE);
        if (uiMessage.getConversationType() == Conversation.ConversationType.GROUP) {
            String extra = gifMessage.getExtra();
            JSONObject json = JSONObject.parseObject(extra);
            if (json != null) {
                try {
                    int age = json.getInteger("age");
                    int gender = json.getInteger("gender");
                    int competence = json.getInteger("competence");
                    viewGender.setUserInfo(gender, age);
                    if (competence > 0) {
                        ivPosition.setImageResource(competence == 1 ? R.mipmap.icon_zuzhang : R.mipmap.icon_fuzuzhang);
                        ivPosition.setVisibility(View.VISIBLE);
                    } else if (competence == -1) {
                        ivPosition.setImageResource(R.mipmap.icon_tourist);
                        ivPosition.setVisibility(View.VISIBLE);
                    } else {
                        ivPosition.setVisibility(View.GONE);
                    }
                    layoutGroup.setVisibility(View.VISIBLE);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            LinearLayout.LayoutParams groupLayoutParams = (LinearLayout.LayoutParams) layoutGroup.getLayoutParams();
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) imageView.getLayoutParams();
            if (uiMessage.getMessageDirection() == Message.MessageDirection.SEND) {
                groupLayoutParams.gravity = Gravity.END;
                layoutParams.gravity = Gravity.END;
            } else {
                groupLayoutParams.gravity = Gravity.START;
                layoutParams.gravity = Gravity.START;
            }
        } else {
            layoutGroup.setVisibility(View.GONE);
        }
    }

    @Override
    protected boolean onItemClick(ViewHolder holder, GIFMessage gifMessage, UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
        ImageView startDownLoad = holder.getView(R.id.rc_start_download);
        if (startDownLoad.getVisibility() == View.VISIBLE && !this.checkPermission(holder.getContext())) {
            applyPermission();
            return true;
        }

        return super.onItemClick(holder, gifMessage, uiMessage, position, list, listener);
    }

    @Override
    protected boolean onItemLongClick(ViewHolder holder, GIFMessage gifMessage, UiMessage uiMessage, int position, List<UiMessage> list, IViewProviderListener<UiMessage> listener) {
        return super.onItemLongClick(holder, gifMessage, uiMessage, position, list, listener);
    }

    private boolean checkPermission(Context context) {
        String[] permission = new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"};
        return PermissionCheckUtil.checkPermissions(context, permission);
    }

    private void applyPermission() {
        String[] permissions = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE};
        PermissionsUtils.getInstance().checkPermission(ActivityManagerUtils.getInstance().getTopActivity(), permissions, "Download", new PermissionsUtils.OnCallBackListener() {
            @Override
            public void open(List<String> list) {

            }

            @Override
            public void notOpen(List<String> list, String label) {

            }
        });
    }
}
